from Accounting.Entity.Base import EntityBase
from Accounting.CashFlow.SubRed import *

from DataAccess.DBConnFactory import DBConnFactory
from datetime import date


class Client(EntityBase):
	def __init__(self, id, parent):
		EntityBase.__init__(self, id, parent)
		
	def purchase_fund(self, purchase_action):
		conn = DBConnFactory().get_db_connection('ACCOUNTING')
		conn.cursor().execute(CASH_CHG_INSERT_SQL, (purchase_action.reference_date().isoformat(),
													self.get_entity_id(),
													purchase_action.amount(),
													purchase_action.currency(),
													"subscription",
													'SWITCH_IN'))
		conn.commit()
		
		if self.parent:  #has parent node
			self.parent.purchase_fund(purchase_action)
		
	def redeming_fund(self, redemption_action):
		if self.parent:  #has parent node
			self.parent.redeming_fund(redemption_action)

		conn = DBConnFactory().get_db_connection('ACCOUNTING')
		conn.cursor().execute(CASH_CHG_INSERT_SQL, (redemption_action.reference_date().isoformat(),
													self.get_entity_id(),
													-redemption_action.amount(),
													redemption_action.currency(),
													"redemption",
													'SWITCH_OUT'))
		conn.commit()
			
		
	def settlement(self, ref_date, invested_asset_val, curncy):
		#print 'debug, settlement at', self.id 
	
		self.create_fee(ref_date)
		self.impl_calc_category_fee_sum('PreIAVFee')
		self.impl_calc_category_fee_sum('PreGAVFee')	
		self.impl_calc_category_fee_sum('PreNAVFee')
		
		self.fee_settlement(ref_date)
		
		
		

		
		
		
	
	
